package com.sale.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.system.api.model.MaterialUnitDto;
import com.sale.system.domain.dto.SysAuxiliaryUnitDateDetailDto;
import com.sale.system.domain.dto.SysTheDropDto;
import com.sale.system.domain.dto.SysUnitDto;
import com.sale.system.domain.vo.SysAuxiliaryUnitVo;
import com.sale.system.service.ISysAuxiliaryUnitService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: YSL
 * @Date: 2023/7/13 11:00
 * @Desc: 多计量单位
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/auxiliaryUnit")
public class SysAuxiliaryUnitController extends BaseController {

    /**
     * 多计量单位
     */
    private final ISysAuxiliaryUnitService sysAuxiliaryUnitService;


    /**
     * 分页获取多计量单位
     *
     * @param sysAuxiliaryUnitVo
     * @param pageQuery
     * @return
     */
    //@SaCheckPermission("system:auxiliaryUnit:list")
    @GetMapping("/list")
    public TableDataInfo<SysAuxiliaryUnitDateDetailDto> selectMultiUnitDetailList(SysAuxiliaryUnitVo sysAuxiliaryUnitVo, PageQuery pageQuery) {
        return sysAuxiliaryUnitService.selectMultiUnitDetailList(sysAuxiliaryUnitVo, pageQuery);
    }

    /**
     * 根据id查询多计量单位
     *
     * @param unitId
     * @return
     */
    //@SaCheckPermission("system:auxiliaryUnit:getMultiUnit")
    @GetMapping(value = "/{unitId}")
    public R<SysAuxiliaryUnitDateDetailDto> getMultiUnit(@PathVariable Long unitId) {
        return R.ok(sysAuxiliaryUnitService.getMultiUnit(unitId));
    }

    /**
     * 根据物料编码查询多计量单位
     */
    @GetMapping(value = "/getDateByCode")
    public R<SysUnitDto> getDateByCode(String materialCode) {
        return R.ok(sysAuxiliaryUnitService.getDateByCode(materialCode));
    }

    /**
     * 新增多计量单位
     *
     * @param sysAuxiliaryUnitVo
     * @return
     */
    //@SaCheckPermission("system:auxiliaryUnit:add")
    @Log(title = "多计量单位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> insertMultiUnit(@Validated @RequestBody SysAuxiliaryUnitVo sysAuxiliaryUnitVo) throws Exception {
        return toAjax(sysAuxiliaryUnitService.insertMultiUnit(sysAuxiliaryUnitVo));
    }

    /**
     * 修改多计量单位
     *
     * @param sysAuxiliaryUnitVo
     * @return
     */
    //@SaCheckPermission("system:auxiliaryUnit:edit")
    @Log(title = "多计量单位", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public R<Void> updateMultiUnit(@Validated @RequestBody SysAuxiliaryUnitVo sysAuxiliaryUnitVo) throws Exception {
        return toAjax(sysAuxiliaryUnitService.updateMultiUnit(sysAuxiliaryUnitVo));
    }

    /**
     * 删除多计量单位
     *
     * @param unitId
     * @return
     */
    //@SaCheckPermission("system:auxiliaryUnit:deleteMultiUnit")
    @Log(title = "多计量单位", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitId}")
    public R<Void> deleteMultiUnit(@PathVariable Long unitId) {
        return toAjax(sysAuxiliaryUnitService.deleteMultiUnit(unitId));
    }

    /**
     * 多计量单位下拉
     *
     * @return
     */
    @GetMapping("/multiMeteringUnitList")
    public R<List<SysTheDropDto>> selectMultiMeteringUnitList() {
        return R.ok(sysAuxiliaryUnitService.selectMultiMeteringUnitList());
    }

    /**
     * 根据物料id获取物料单位（dubbo调用）
     */
    @GetMapping("/getUnitDataByCode")
    public R<MaterialUnitDto> getUnitDataByCode(Long id) {
        return R.ok(sysAuxiliaryUnitService.getUnitDataByCode(id));
    }


    /**
     * 多单位换算
     *
     * @return
     */
    @GetMapping("/selectMaterialUnitList")
    public R<MaterialUnitDto> selectMaterialUnitList(Long id, Long unitQuantity) {
        return R.ok(sysAuxiliaryUnitService.selectMaterialUnitList(id, unitQuantity));
    }

    /**
     * 根据多计量单位id查询详情
     *
     * @return
     */
    @GetMapping("/getMaterialUnitById/{unitId}")
    public SysAuxiliaryUnitDateDetailDto getMaterialUnitById(@PathVariable Long unitId) throws Exception {
        return sysAuxiliaryUnitService.getMaterialUnitById(unitId);
    }

    /**
     * 根据物料编码查询出单位信息和辅助单位(返回map)
     */
    @GetMapping("getUnitByCodeToMap")
    public Map<String, MaterialUnitDto> getUnitByCodeToMap() {
        return sysAuxiliaryUnitService.getUnitByCodeToMap();
    }

    /**
     * 返回多单位map集合(报表模块使用)
     */
    @GetMapping("getUnitToMap")
    public Map<Long, String> getUnitToMap() {
        return sysAuxiliaryUnitService.getUnitToMap();
    }

}
